package simple.alert.rule.expression;

import java.util.List;

import simple.alert.message.AlarmLevelMessage;
import simple.alert.rule.condition.ICondition;
import simple.alert.rule.group.GroupOperator;

public class Expression {

	private GroupOperator groupOperator;
	
	private List<ICondition> conditionList;
	
	private AlarmLevelMessage alarmLevelMessage;

	
	public Expression(List<ICondition> conditionList, AlarmLevelMessage alarmLevelMessage) {
		super();
		if(conditionList.size() != 1) {
			throw new IllegalArgumentException("condition is allowed only 1 when group operator is not applied.");
		}
		this.conditionList = conditionList;
		this.alarmLevelMessage = alarmLevelMessage;
	}
	
	public Expression(GroupOperator groupOperator,
			List<ICondition> conditionList, AlarmLevelMessage alarmLevelMessage) {
		super();
		this.groupOperator = groupOperator;
		if(conditionList.size()<2) {
			throw new IllegalArgumentException("condition must great than 2 when group operator is applied.");
		}
		this.conditionList = conditionList;
		this.alarmLevelMessage = alarmLevelMessage;
	}


	public AlarmLevelMessage eval(long input) {
		if(groupOperator != null) {
			if(groupOperator.calc(conditionList, input)) {
				return alarmLevelMessage;
			}
			else {
				return AlarmLevelMessage.OK;
			}
		}
		else {
			if(conditionList.get(0).isMatch(input)) {
				return alarmLevelMessage;
			}
			else {
				return AlarmLevelMessage.OK;
			}
		}
	}
	
}
